﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QLST
{
    public partial class NhanVienUI : Form
    {
        public NhanVienUI()
        {
            InitializeComponent();
        }

        private void NhanVien_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        SqlConnection conn = new SqlConnection();
        String strConn = "Data Source=MRBREAK\\SQLEXPRESS;Initial Catalog=QLSieuThi;Integrated Security=True";

        public void LoadData()
        {
            conn = new SqlConnection(strConn);
            conn.Open();
            string str = "Select NhanVien.*, NguoiDung.ID, NguoiDung.Pass, NguoiDung.Role from NhanVien,NguoiDung where NhanVien.MaNV=NguoiDung.MaNV";
            SqlDataAdapter da = new SqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            grvShow.DataSource = dt;
            conn.Close();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(strConn);
            conn.Open();
            string str = "Insert into NhanVien(MaNV,TenNV,NgaySinh,Phone,DiaChi) values ('" + txtMaNv.Text + "','" + txtTen.Text + "','" + (dtpNs.Text) + "','" + txtPhone.Text + "','" + txtDc.Text + "')"
                            + "Insert into NguoiDung(MaNV,ID,Pass,Role) values ('" + txtMaNv.Text + "','" + txtUName.Text + "','" + txtPass.Text + "','" + txtRole.Text + "')";
            SqlCommand cmd = new SqlCommand(str, conn);
            cmd.ExecuteNonQuery();
            LoadData();
            conn.Close();           
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(strConn);
            conn.Open();
            if (txtMaNv.Text != "")
            {
                string str = "Update NhanVien set TenNV='" + txtTen.Text + "',NgaySinh='" + (dtpNs.Text) + "',Phone='" + txtPhone.Text + "',DiaChi='" + txtDc.Text + "' where MaNV = '"+txtMaNv.Text+"'" ;
                SqlCommand cmd = new SqlCommand(str, conn);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Cập nhật thành công!!!");
                conn.Close();
                LoadData();
            }
            else
            {
                MessageBox.Show("Bạn chưa nhập mã nhân viên...", "Thông báo");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(strConn);
            conn.Open();
            string str = "Delete from NhanVien where MaNV='" + txtMaNv.Text + "'";
            SqlCommand cmd = new SqlCommand(str, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            try
            {
                txtMaNv.Clear();
                txtTen.Clear();
                txtPhone.Clear();
                txtDc.Clear();
                MessageBox.Show("Xóa thành công!!!");
                grvShow.ClearSelection();
            }
            catch
            {
                MessageBox.Show("Xóa không thành công!!!");
            }
            LoadData();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            DialogResult = MessageBox.Show("Bạn có muốn thoát không ?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (DialogResult == DialogResult.OK)
            {
                Application.Exit();
            }
        }
    }
}
